package com.study.networkProgram;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;

public class Dom {

    public void parse() throws IOException, SAXException, ParserConfigurationException {
        InputStream inputStream = Demo.class.getResourceAsStream("/note.xml");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dbf.newDocumentBuilder();

        Document document = builder.parse(inputStream);
        NodeList nodeList = document.getElementsByTagName("Student");
        for(int i = 0; i < nodeList.getLength(); i++){
            Element node = (Element) nodeList.item(i);
            System.out.println("Name:" + node.getElementsByTagName("Name").item(0).getFirstChild().getNodeValue());
        }
    }
}
